//
//  YLCollectionCalculatorHeader.m
//  YLFinanceService
//
//  Created by she1323 on 2017/9/15.
//  Copyright © 2017年 she1323. All rights reserved.
//  收款计算器

#import "YLCollectionCalculatorHeader.h"
#import <Masonry.h>
#import "YLGlobeHeader.h"

@interface YLCollectionCalculatorHeader ()<UITextFieldDelegate>

@property (nonatomic, assign) NSUInteger beishu;

@property (nonatomic, assign) NSInteger sum;

@property (nonatomic, weak) UITextField *textField;

@end

@implementation YLCollectionCalculatorHeader

- (instancetype)initWithFrame:(CGRect)frame
{
    if (self = [super initWithFrame:frame]) {
        self.beishu = 10000;
        [self setUpUI];
        self.backgroundColor = [UIColor whiteColor];
    }
    return self;
}

// 71 401
- (void)setUpUI
{
    
    UITextField *textField = [[UITextField alloc] init];
    _textField = textField;
    textField.delegate = self;
    textField.textColor = YLMainColor;
    textField.textAlignment = NSTextAlignmentCenter;
    textField.keyboardType = UIKeyboardTypeNumberPad;
    textField.borderStyle = UITextBorderStyleNone;
    [self addSubview:textField];
    textField.background = [UIImage imageNamed:@"numberBorder"];
    __weak typeof(self) weakSelf = self;
    [textField mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(weakSelf).mas_offset(17);
        make.bottom.mas_equalTo(weakSelf).mas_offset(-17);
        make.width.mas_equalTo(textField.mas_height).multipliedBy(401 / 66.0);
        make.centerX.mas_equalTo(weakSelf);
    }];
    
    UIButton *addButton = [self buttonWithTitle:@"+" action:@selector(add)];
    
    [addButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(textField.mas_right).mas_offset(10);
        make.top.mas_equalTo(textField).mas_offset(2);
        make.bottom.mas_equalTo(textField).mas_offset(-2);
        make.width.mas_equalTo(addButton.mas_height);
    }];
    
    
    
    UIButton *minusButton = [self buttonWithTitle:@"-" action:@selector(minus)];
    
    [minusButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.mas_equalTo(textField.mas_left).mas_offset(-10);
        make.top.mas_equalTo(textField).mas_offset(2);
        make.bottom.mas_equalTo(textField).mas_offset(-2);
        make.width.mas_equalTo(minusButton.mas_height);
    }];
    
}

- (void)setLeastMoney:(NSInteger)leastMoney
{
    _leastMoney = leastMoney;
    [self setSum:leastMoney];
    
}

- (void)setSum:(NSInteger)sum
{
    _sum = sum;
        
    if (self.moneyChangeBlock) {
        self.moneyChangeBlock(sum);
    }
    
    _textField.text = [NSString stringWithFormat:@"%zd", sum];
    
}

- (void)textFieldDidEndEditing:(UITextField *)textField
{
    NSInteger sum = [textField.text integerValue];
    if (sum <= self.leastMoney) {
        [self setSum:self.leastMoney];
    } else if (sum >= 10000000000){
        [self setSum:10000000000];
    }  else {
        [self setSum:[textField.text integerValue] / 10000 * 10000];
    }
}



// 加
- (void)add
{
    self.sum = self.sum + _beishu;
}

// 减
- (void)minus
{
    if (self.sum <= self.leastMoney) {
        return;
    }
    self.sum = self.sum - _beishu;
}

- (UIButton *)buttonWithTitle:(NSString *)title action:(SEL)action
{
    UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
    [button setTitleColor:YLColor(66, 66, 66) forState:UIControlStateNormal];
    [self addSubview:button];
    [button setBackgroundImage:[UIImage imageNamed:@"buttonBorder"] forState:UIControlStateNormal];
    [button setTitle:title forState:UIControlStateNormal];
    [button addTarget:self action:action forControlEvents:UIControlEventTouchUpInside];
    return button;
}
@end
